package ar.edu.unlu.sistemas.movirdroid.business.module;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Random;

import ar.edu.unlu.sistemas.movirdroid.android.AppService;
import ar.edu.unlu.sistemas.movirdroid.business.index.IndexController;
import ar.edu.unlu.sistemas.movirdroid.business.index.Indexer;
import ar.edu.unlu.sistemas.movirdroid.business.util.Logger;
import ar.edu.unlu.sistemas.movirdroid.business.util.Params;
import ar.edu.unlu.sistemas.movirdroid.business.vo.WordVO;

public  class TestModule {
	
	private static IndexController index = IndexController.getInstance();
	private static AppService appService = AppService.instance;

	private static void cleanIndex() {
		index.deleteTf(null);
		index.deleteDoc(null);
		index.deleteWord(null);
		index.initSequences();
	}
	
	public static void saveIndex() {
		index.saveIndex();
	}
	
	public static Integer vocabularySize() {
		return appService.findWords(null).size();
	}
	
	public static long[] reIndexFiles(Integer nFiles) {
		appService.indexUpdateLaunched = true;
		cleanIndex();
		File dir = new File("/mnt/sdcard/"+Params.CORPUS_DIR);
		File[] fileArr = dir.listFiles();
		File file;
		Long bytes = new Long(0);
		Long pFiles = new Long(0);
		long begin = System.currentTimeMillis();
		if (fileArr != null) {
			if ((nFiles==null) || (nFiles > fileArr.length)) {
				nFiles = fileArr.length;
			}
			for (int i=0;i<=nFiles-1;i++) {
				try {
					file = fileArr[i];
					Logger.logDebug(TestModule.class.getSimpleName(), " Indexo "+file.getName());
					FileReader fr = new FileReader(file);
					Indexer.getInstance().addFile(fr, file.getName());
					bytes += file.length();
					pFiles++;
				} catch (FileNotFoundException fnfe) {
					Logger.logError(TestModule.class.getSimpleName(), " Error agregando documento",fnfe);
				}
				
			}
		}
		index.saveIndex();
		appService.indexUpdateLaunched = false;
		return new long[]{pFiles.longValue(),bytes.longValue(),System.currentTimeMillis() - begin};
	}
	
	public static String queryTest(Integer nFiles) {
		String nFilesStr = String.valueOf(nFiles);
		int nWords = index.findAllWords().size();
		Random random = new Random();
		int id;
		WordVO word;
		long beginMillis;
		int nResult ;
		StringBuffer resultBuffer = new StringBuffer();
		resultBuffer.append("|OK|\n");
		int id1;
		StringBuffer queryBuffer;
		int id2;
		for (int i = 1; i<= 30; i++) {
			id	= random.nextInt(nWords);
			word = index.findWordById(id);
			beginMillis = System.currentTimeMillis();
			nResult = appService.processQueryLocal(word.getName()).size();
			resultBuffer.append(nFilesStr+"|1|"+String.valueOf(System.currentTimeMillis() - beginMillis)+"|"+id+"|"+nResult+"\n");
		}
		for (int i = 1; i<= 30; i++) {
			queryBuffer = new StringBuffer();
			id	= random.nextInt(nWords);
			word = index.findWordById(id);
			queryBuffer.append(word.getName()+" ");
			id1	= random.nextInt(nWords);
			while (id == id1) {
				id1	= random.nextInt(nWords);
			}
			word = index.findWordById(id1);
			queryBuffer.append(word.getName());
			beginMillis = System.currentTimeMillis();
			nResult = appService.processQueryLocal(queryBuffer.toString()).size();
			resultBuffer.append(nFilesStr+"|2|"+String.valueOf(System.currentTimeMillis() - beginMillis)+"| & "+id+"-"+id1+"|"+nResult+"\n");
		}
		for (int i = 1; i<= 30; i++) {
			queryBuffer = new StringBuffer();
			id	= random.nextInt(nWords);
			word = index.findWordById(id);
			queryBuffer.append(word.getName()+" ");
			id1	= random.nextInt(nWords);
			while (id == id1) {
				id1	= random.nextInt(nWords);
			}
			word = index.findWordById(id1);
			queryBuffer.append(word.getName()+" ");
			id2	= random.nextInt(nWords)+1;
			while (id2 == id1 || id2 == id ) {
				id2 = random.nextInt(nWords)+1;
			}
			word = index.findWordById(id2);
			queryBuffer.append(word.getName());
			beginMillis = System.currentTimeMillis();
			nResult = appService.processQueryLocal(queryBuffer.toString()).size();
			resultBuffer.append(nFilesStr+"|3|"+String.valueOf(System.currentTimeMillis() - beginMillis)+"| & "+id+"-"+id1+"-"+id2+"|"+nResult+"\n");
		}
		return resultBuffer.toString();
	}
	
}
